package com.atguigu.lease.web.admin.controller.apartment;


import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.LabelInfo;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.service.LabelInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Tag(name = "标签管理")
@RestController
@RequestMapping("/admin/label")
public class LabelInfoController {
    @Autowired
    private LabelInfoService labelInfoService;

    //select * from label_info where type=1
    //1表示公寓标签,2表示房间标签
    //正常可以这样写,也可以将变量的类型封装成枚举类
    @Operation(summary = "（根据类型）查询标签列表2")
    @GetMapping("list2")
    public Result list2(@RequestParam Integer type) {
        LambdaQueryWrapper<LabelInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(LabelInfo::getType, type);
        List<LabelInfo> list = labelInfoService.list(lambdaQueryWrapper);
        return Result.ok(list);
    }
    //方式2: 为LabelInfo的type属性赋值: 用枚举类型ItemType
    //会报400错误,类型转换异常,不能将String转成枚举
    //  解决: 创建转换类 实现Converter接口,用于将HTTP请求参数String转成枚举ItemType
    //      可以用泛型,可以将String转成任意枚举类型
    //      还要将其注册到SpringMVC中,项目一启动就加载
    @Operation(summary = "（根据类型）查询标签列表")
    @GetMapping("list")
    public Result list(@RequestParam(required = false,name = "type") ItemType itemType) {
        log.info("itemType: {}",itemType);

        LambdaQueryWrapper<LabelInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(itemType != null,LabelInfo::getType, itemType);
        List<LabelInfo> list = labelInfoService.list(lambdaQueryWrapper);
        return Result.ok(list);
    }

    @Operation(summary = "新增或修改标签信息")
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdate(@RequestBody LabelInfo labelInfo) {
        boolean isSuccess = labelInfoService.saveOrUpdate(labelInfo);
        if (isSuccess) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @Operation(summary = "根据id删除标签信息")
    @DeleteMapping("deleteById")
    public Result deleteLabelById(@RequestParam Long id) {
        boolean isSuccess = labelInfoService.removeById(id);
        if (isSuccess) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }
}
